<?php
namespace App\Repository;

use App\Model\Order;
use App\Facade\Event;
use App\Event\Order\StatusOkEvent;
use App\Event\Order\StatusNoEvent;

class OrderRepository extends \HyperfStore\Repository
{
    // 放入模型
    public function build()
    {
        return new Order;
    }

    // 创建
    public function create(UserRepository $user, ProductRepository $product, $data)
    {
        $this->model->user()->associate($user->model());
        $this->model->product()->associate($product->model());
        $this->model->level()->associate($product->level);
        
        $data['product_name'] = $product->name;
        $data['product_image'] = $product->images[0] ?? '';
        
        return $this->exception(function() use($data) {
            return $this->save($data);
        });
    }
    
    // 需要支付的金额
    public function payMoney()
    {
        return $this->model->money;
    }
    
    // 发货操作
    public function setExpress($name, $code)
    {
        if($this->statusPay())
        {
            $this->model->express = $name ?? '';
            $this->model->express_code = $code ?? '';
            
            // 如果状态已完成，将不用修改
            if(!$this->statusOk())
            {
                $this->model->status = Order::STATUS_EXPRESS;
            }
            
            return $this->model->save();
        }
    }
    
    // 判断订单已支付
    public function statusPay()
    {
        return $this->model->status > Order::STATUS_POST;
    }
    
    // 判断是否完成订单
    public function statusOk()
    {
        return $this->model->status == Order::STATUS_OK;
    }
    
    // 判断是否关闭
    public function statusNo()
    {
        return $this->model->status == Order::STATUS_NO;
    }
    
    // 设置已支付
    public function setStatusPay($pay)
    {
        if(!$this->statusOk())
        {
            $this->model->status = Order::STATUS_PAY;
            $this->model->pay = $pay;
            $this->model->pay_time = time();
        
            return $this->model->save();
        }
    }
    
    // 确认收货
    public function setStatusOk()
    {
        if(!$this->statusOk() && $this->statusPay())
        {
            Event::dispatch(new StatusOkEvent($this));
            
            $this->model->status = Order::STATUS_OK;
            $this->model->ok_time = time();
                
            return $this->model->save();
        }
    }
    
    // 关闭订单
    public function setStatusNo()
    {
        if(!$this->statusOk() && !$this->statusNo())
        {
            Event::dispatch(new StatusNoEvent($this));
            
            $this->model->status = Order::STATUS_NO;
        
            return $this->model->save();
        }
    }
    
    // 设置释放积分
    public function setMoneyCount($money)
    {
        $this->model->money_count = $money;
        $this->model->money_remain = $money;
        
        
        return $this->model->save();
    }
    
    // 获取释放积分，传入比例和加速
    public function getMoneyRemain($config, $speed)
    {
        // 初始比例 * 加速度
        $config = bcmul((float) $config, $speed / 100, 3);
        
        // 算出释放金额
        $money = bcmul($this->model->money_count, (float) $config / 100, 2);
        
        // 对比最小
        return min($money, $this->model->money_remain);
    }
    
    // 手动释放积分
    public function makeMoneyRemain($money)
    {
        $money = min($money, $this->model->money_remain);
        
        $this->model->decrement('money_remain', min($money, $this->model->money_remain));
        
        return $money;
    }
}